<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/layer/layer.min.js"></script>
    <script type="text/javascript" src="/js/H-ui.js"></script>
    <script type="text/javascript" src="/js/H-ui.admin.js"></script>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/js/html5.js"></script>
    <script type="text/javascript" src="/js/respond.min.js"></script>
    <script type="text/javascript" src="/js/PIE_IE678.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="/css/H-ui.css"/>
    <link type="text/css" rel="stylesheet" href="/css/H-ui.admin.css"/>
    <link type="text/css" rel="stylesheet" href="/font/font-awesome.min.css"/>
    <!--[if IE 7]>
    <link href="/font/font-awesome-ie7.min.css" rel="stylesheet" type="text/css"/>
    <![endif]-->
    <!--    <script src="/zTreev3/js/jquery-1.4.4.min.js"></script>-->
    <!--bootstrap-->
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/bootstrap/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="/zTreev3/css/zTreeStyle/zTreeStyle.css"/>
    <script src="/zTreev3/js/jquery.ztree.all.js"></script>
    <!--bootstrap-dialog-->
    <script type="text/javascript" src="/js/dist/js/bootstrap-dialog.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/dist/css/bootstrap-dialog.css"/>
    <!--bootstrap-toastr-->
    <script type="text/javascript" src="/js/toastr/toastr.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/toastr/toastr.css" />
    <title>菜单管理</title>
</head>
<style type="text/css">
    .modal.fade.in {
        top: 90px;
        right: 100px;
    }
</style>
<body>

<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
</div>


</body>
<script>
    // <!--
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            autoCheckTrigger: true
        },
        async: {
            enable: true,
            type: "get",
            url: "/menu/zTree",
            autoParam: ["id"]
        },
        view: {
            addHoverDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + "_a");
                if (treeNode.editNameFlag || $("#btnGroup" + treeNode.tId).length > 0) return;
                var s = '<span id="btnGroup' + treeNode.tId + '">';
                if (treeNode.level == 0) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 1) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    if (treeNode.children == undefined) {
                        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#" >删除</a>';
                    }
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode(' + treeNode.id + ')" href="#" >添加</a>';
                } else if (treeNode.level == 2) {
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode(' + treeNode.id + ')" href="#" title="修改权限信息">修改</a>';
                    s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode(' + treeNode.id + ')" href="#">删除</a>';
                }
                s += '</span>';
                aObj.after(s);
            },
            removeHoverDom: function (treeId, treeNode) {
                $("#btnGroup" + treeNode.tId).remove();
            }
        }
    };

    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting);
    });


    function addNode(nodeId) {
        menu_add('800', '500', '菜单添加', '/menu/toAdd/' + nodeId);

    }

    function editNode(nodeId) {
        menu_edit('800', '500', '菜单修改', '/menu/toUpdate/' + nodeId);
        // $.fn.zTree.init($("#treeDemo"), setting);
    }

    function deleteNode(nodeId) {
        var flag = confirm("是否要删除?");
        if (flag) {
            $.ajax({
                url: "/menu/"+nodeId,
                dataType: "json",
                type: "delete",
                success: function (data) {
                    if (data.code == 200){
                        toastr.success("删除成功");
                        $.fn.zTree.init($("#treeDemo"), setting);
                    }else{
                        toastr.error("异常,请联系管理员");
                    }
                },
                error: function (e) {
                    alert("系统错误");
                }
            })
        }
    }
</script>
</html>